var { verifytoken } = require('../generate_token/generateToken')
// 验证短koken
exports.short_checkAuth = function (secretKey) {
    return (req, res, next) => {
        const { authorization } = req.headers;
        if (!authorization) {
            return res.send({
                code: 1001,
                message: "您没有权限，当前您没有携带token"
            })
        } else {
            const short = authorization.split(" ")[0];
            let token = short.split("+")[1];
            const claim = verifytoken(token, secretKey);
            if (claim === null) {
                return res.send({
                    code: 1002,
                    message: "您没有权限，短token过期"
                })
            } else {
                req.claim = claim;
                next();//如果token验证通过，则执行下一个
            }
        }
    }
}


// 验证长token
exports.length_checkAuth = function (secretKey) {
    return (req, res, next) => {
        const { authorization } = req.headers;
        if (!authorization) {
            return res.state(1003).send({
                code: 1003,
                message: "短token过期，当前没有携带长token"
            })
        } else {
            const length_token = authorization.split(" ")[1];
            const token = length_token.split("+")[1];
            const claim = verifytoken(token, secretKey);
            if (claim === null) {
                return res.send({
                    code: 1004,
                    message: "长token过期，请重新登录"
                })
            } else {
                req.claim = claim;
                next();//如果token验证通过，则执行下一个
            }
        }
    }
}